package com.hunliji.monitor.dao.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import com.hunliji.monitor.dao.interceptor.SqlLogMybatisInterceptor;
import lombok.Data;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;

/**
 * 数据库配置
 * @author wuchen
 */
@Configuration
@MapperScan(basePackages = "com.hunliji.monitor.dao.mapper.wedding", sqlSessionTemplateRef  = "weddingSqlSessionTemplate")
@ConfigurationProperties(prefix = "spring.datasource.wedding.druid")
@Data
public class WeddingDataSourceConfig {
    /** 数据库的 host **/
    private String url;
    private String username;
    private String password;
    private String driverClassName;
    /**初始化的时候设定的连接数**/
    private int initialSize;
    /** 最小空闲连接数 **/
    private int minIdle;
    /** 最大连接数 **/
    private int maxActive;
    /** 最大等待毫秒数, 单位为 ms, 超过时间会出错误信息 **/
    private long maxWait;

    @Bean(name = "weddingDataSource",initMethod = "init")
    public DataSource businessDataSource() {
        DruidDataSource druid = new DruidDataSource();
        // 配置基本属性
        druid.setDriverClassName(driverClassName);
        druid.setUsername(username);
        druid.setPassword(password);
        druid.setUrl(url);

        //初始化时建立物理连接的个数
        druid.setInitialSize(initialSize);
        //最大连接池数量
        druid.setMaxActive(maxActive);
        //最小连接池数量
        druid.setMinIdle(minIdle);
        //获取连接时最大等待时间，单位毫秒。
        druid.setMaxWait(maxWait);

        return druid;
    }

    @Bean(name = "weddingSqlSessionFactory")
    public SqlSessionFactory businessSqlSessionFactory(@Qualifier("weddingDataSource") DataSource dataSource) throws Exception {
        MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setMapperLocations(
                new PathMatchingResourcePatternResolver().getResources("classpath*:/mapper/wedding/*Mapper.xml"));
        return bean.getObject();
    }

    @Bean(name = "weddingTransactionManager")
    public DataSourceTransactionManager businessTransactionManager(@Qualifier("weddingDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "weddingSqlSessionTemplate")
    public SqlSessionTemplate businessSqlSessionTemplate(@Qualifier("weddingSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
    /**
     * 添加拦截器
     * @param sqlSessionFactory
     * @return
     */
    @Bean
    public String myInterceptor(@Qualifier("weddingSqlSessionFactory")SqlSessionFactory sqlSessionFactory) {
        //实例化插件
        SqlLogMybatisInterceptor sqlInterceptor = new SqlLogMybatisInterceptor();
        //将插件添加到SqlSessionFactory工厂
        sqlSessionFactory.getConfiguration().addInterceptor(sqlInterceptor);
        return "interceptor";
    }


}
